package com.lagou.dao;

import com.lagou.domain.User;
import com.lagou.utils.C3P0Utils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import javax.sql.DataSource;
import java.sql.SQLException;

public class UserDaoImpl implements UserDao{

    //1.用户登录方法
    @Override
    public User login(String username, String password) throws SQLException {

        QueryRunner queryRunner = new QueryRunner(C3P0Utils.dataSource);
        String sql = "select * from t_user where username = ? and passwd = ?";
        Object[] param = {username,password};
        System.out.println(sql);
        return queryRunner.query(sql,new BeanHandler<>(User.class),param);
    }

    //2.用户添加方法
    @Override
    public void addUser(User user) throws SQLException{

            QueryRunner queryRunner = new QueryRunner(C3P0Utils.dataSource);
            String sql = "insert into t_user(userid,username,passwd,sex,birthday) values(?,?,?,?,?)";
            Object[] param = {user.getUserid(),user.getUsername(),user.getPassword(),user.getSex(),user.getBirthday()};
            System.out.println(sql);
            queryRunner.update(sql,param);
    }
}
